[50分]ajax简单问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 18:10:41
第一个:1.jsp文件

---------------我想将结果变成一个xml文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="application/xml; charset=UTF-8"%>
<%
out.println("<?xml version='1.0' encoding='UTF-8'?>");
out.println("<test>");
out.println("abcd");
out.println("</test>");
out.println("</xml>");
%>
------------------
页面2来解析上面那个:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
<head>

</head>
<script type="text/javascript">
var xmlHttp;
var xmlDoc;
function fn(){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET","1.jsp",tru

这样`xmlDoc=xmlHttp.responseXML应该是空的,得不到XML的,返回的XML应该用response.getWriter().write(last_xml)写出来,last_xml是一个String是你要写出的XML而且不用<?xml version='1.0' encoding='UTF-8'?>
给个例子你看一下吧:我是写的servlet不是JSP,不过应该差不多.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType( "text/xml; charset=UTF-8" );
response.setHeader("Cache-Control", "no-cache");
String xml_start = "<file>";
String xml_end = "</file>";
String xml = "";
xml += "<fileCode>0</fileCode>";

String last_xml = xml_start + xml + xml_end;

response.getWriter().write(last_xml);
}
希望能够帮助你